package com.ty.thymeleaf.controller;

import com.ty.thymeleaf.entity.User;
import com.ty.thymeleaf.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.List;

/**
 * ExampleController
 *
 * @aurhor Administrator  whs
 * @since 2024/9/12
 */
@Controller
public class ExampleController {

    @Resource
    private UserService userService;

    @GetMapping(value = "/hello/{id}")
    public String getUser(@PathVariable("id") Long usrId, Model model) {
        User user = userService.getUser(usrId);
        model.addAttribute("user", user);
        return "demo/hello";
    }

    /**
     * 变量的赋值和拼接
     */
    @GetMapping(value = "/string")
    public String getString(Model model , HttpServletRequest request) { // 演示赋值、字符串拼接
        model.addAttribute("userName", "小恐龙 阿贡 🦖");

        request.setAttribute("test", "request");
        request.getSession().setAttribute("test", "session");
        model.addAttribute("date", new Date());

        User user = userService.getUser(1l);

        model.addAttribute("user",  user);
        return "demo/string";
    }

    /**
     * inline 文本内联
     */
    @GetMapping(value = "/inline")
    public String inline(Model model) { // 演示赋值、字符串拼接
        model.addAttribute("userName",  "小恐龙 阿贡 🦖");
        return "demo/inline";
    }

    /**
     * if 文本内联
     */
    @GetMapping(value = "/if")
    public String ifUnless(Model model) { // 演示 条件判断 if/unless
        model.addAttribute("flag",  "yes");
        model.addAttribute("小恐龙",  "小恐龙 阿贡 🦖");
        model.addAttribute("sex",  "man");  // case 循环
        return "demo/if";
    }

    /**
     *  list for 循环
     */
    @GetMapping(value = "/list")
    public String list(Model model) {
        List<User> list = userService.findAllUsers();
        model.addAttribute("list", list);
        return "demo/list";
    }






}
